/** 
 * Description:商品品牌管理类
 * Date:2013-1-9
 * Author:Along	
*/
package com.community.shopping.web;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.community.shopping.domain.Goods;
import com.community.shopping.domain.GoodsBrand;
import com.community.shopping.domain.GoodsType;
import com.community.shopping.service.logic.GoodsBrandManager;
import com.community.shopping.service.logic.GoodsManager;
import com.community.shopping.service.logic.GoodsTypeManager;
import com.community.shopping.service.logic.Page;
import com.community.shopping.util.RequestHelp;

@Controller 
public class GoodsBrandControl {
	
	@Autowired  
    private GoodsBrandManager goodsBrandManager;
	@Autowired  
    private GoodsTypeManager goodsTypeManager;
	@Autowired  
    private GoodsManager goodsManager;
	
	/**
	 * 品牌列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/brandlist")
	 public ModelAndView list(HttpServletRequest request) 
   { 
    ModelAndView modelAndView = new ModelAndView("goods/goodsbrand_list.jsp"); 
    Page page=new Page(request);		
	String searchKey=request.getParameter("searchKey");
	String type=request.getParameter("type");
	String hql=" 1=1 ";
	if(StringUtils.isNotBlank(searchKey))
	{
		hql=hql+" and (brandName like '%"+searchKey.trim()+"%' )";
	}
	if(StringUtils.isNotBlank(type))
	{
		hql=hql+" and goodsType.typeName  like '%"+type.trim()+"%' ";
	}
	Integer shopId=RequestHelp.getShopId(request);
	List<GoodsBrand> brandlist=goodsBrandManager.getGoodsBrand(hql+" and shopId="+shopId,page);
	modelAndView.addObject("brandlist", brandlist); 
	modelAndView.addObject("page", page); 
   return modelAndView; 		

	  } 
	/**
	 * 添加品牌
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/brandadd")
	 public ModelAndView brandAdd(HttpServletRequest request) 
    { 
     ModelAndView modelAndView = new ModelAndView("goods/goodsbrand_edit.jsp"); 
     Integer shopId=RequestHelp.getShopId(request);
     List topTypes=goodsTypeManager.getTopGoodsType(shopId);
     modelAndView.addObject("toptypes", topTypes); 
     return modelAndView; 		

	  } 
	/**
	 * 修改品牌
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/brandedit")
	 public ModelAndView brandEdit(HttpServletRequest request) 
    { 
     ModelAndView modelAndView = new ModelAndView("goods/goodsbrand_edit.jsp"); 
	  String brandid=request.getParameter("id");
      GoodsBrand brand=goodsBrandManager.getGoodsBrand(Integer.valueOf(brandid));
	  modelAndView.addObject("brand", brand);
	  Integer shopId=RequestHelp.getShopId(request);
	  List topTypes=goodsTypeManager.getTopGoodsType(shopId);
	  modelAndView.addObject("toptypes", topTypes); 
	   
	 //修改商品时保存列表页的查询条件，保存后返回时才能保留在原界面  
	  String searchKey=request.getParameter("searchKey");
	  String type=request.getParameter("type");
	  modelAndView.addObject("searchKey", searchKey); 
	  modelAndView.addObject("type", type);
	  modelAndView.addObject("page", new Page(request)); 
      return modelAndView; 		

	  } 
	/**
	 * 保存品牌基本信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/brandsave")
	 public ModelAndView save(HttpServletRequest request) throws Exception
    {  	
	 String brandId=request.getParameter("id");
	 GoodsBrand brand=new GoodsBrand();
	 //如果是新商品保存，创建存放商品静态页面的目录
	 if(StringUtils.isBlank(brandId))
	 {			
		 Integer shopId=RequestHelp.getShopId(request);
		 brand.setShopId(shopId);
		
	 }else
	 {
		 brand=goodsBrandManager.getGoodsBrand(Integer.parseInt(brandId));
	 }
	 brand.setBrandName(request.getParameter("brandName"));
	 brand.setSort(Integer.parseInt(request.getParameter("sort")));
	 String goodsTypeId=request.getParameter("goodsTypeId");
	 GoodsType goodsType=goodsTypeManager.getGoodsType(Integer.parseInt(goodsTypeId));
	 brand.setGoodsType(goodsType);
	 goodsBrandManager.save(brand);	
	 ModelAndView modelAndView = new ModelAndView("brandlist.do"); 
     return modelAndView; 		
	  } 
	
	/**
	 * 删除品牌
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/brandremove")
	 public void delete(HttpServletRequest request ,HttpServletResponse response) throws Exception  
   {  	
	 String brandId=request.getParameter("id");	
	 List goodsList=goodsManager.getGoods(" brand.id="+brandId);
	 if(goodsList.size()>0)
	 {
	   response.getWriter().print(2); 	 		 
	 } 
	 else {
	   GoodsBrand brand=goodsBrandManager.getGoodsBrand(Integer.parseInt(brandId));	 	
	   goodsBrandManager.delete(brand);
	   response.getWriter().print(1); 	
	 }
	
   }
	
	/**
	 * 查询某商品类别下的所有品牌
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/querybrand")
	 public void query(HttpServletRequest request ,HttpServletResponse response) throws Exception  
   {  	
	 String typeId=request.getParameter("typeId");	
	 List<GoodsBrand> brandlist=goodsBrandManager.getGoodsBrandByType(Integer.parseInt(typeId));
	 StringBuilder sb=new StringBuilder("[");
		for(GoodsBrand brand:brandlist)
		{
			if(sb.length()>10)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"id\":\""+brand.getId()+"\",");
			sb.append("\"name\":\""+brand.getBrandName()+"\"");
			sb.append("}");		
		}
		sb.append("]");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 		
	
   }
	
}
